package com.cskaoyan.shopping.controller;

import com.cskaoyan.mall.commons.result.ResponseData;
import com.cskaoyan.mall.commons.result.ResponseUtil;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.shopping.dto.*;
import com.cskaoyan.shopping.service.IContentService;
import com.cskaoyan.shopping.service.IHomeService;
import com.cskaoyan.shopping.service.IProductCateService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Set;

/**
 * 创建日期: 2022/05/18 22:37
 *
 * @author liutao
 */
@RestController
public class ShoppingController {

    @Autowired
    IContentService iContentService;

    @Autowired
    IHomeService iHomeService;

    @Autowired
    IProductCateService iProductCateService;

    @GetMapping("/shopping/homepage")
    public ResponseData shoppingHomepage() {

        // PanelContentItemDtoListResponse response = iContentService.queryPanelContentItemList();
        HomePageResponse homepage = iHomeService.homepage();

        if (ShoppingRetCode.SUCCESS.getCode().equals(homepage.getCode())) {
            // 业务方法调用成功
            Set<PanelDto> panelContentItemDtos = homepage.getPanelContentItemDtos();
            return new ResponseUtil().setData(panelContentItemDtos);
        }

        // 业务方法执行失败
        return new ResponseUtil().setErrorMsg(homepage.getMsg());
    }

    @GetMapping("/shopping/navigation")
    public ResponseData shoppingNavigation() {

        NavListResponse navListResponse = iContentService.queryNavList();
        if (ShoppingRetCode.SUCCESS.getCode().equals(navListResponse.getCode())) {
            // 业务方法调用成功
            List<PanelContentDto> pannelContentDtos = navListResponse.getPannelContentDtos();
            return new ResponseUtil().setData(pannelContentDtos);
        }

        // 业务方法执行失败
        return new ResponseUtil().setErrorMsg(navListResponse.getMsg());
    }

    @GetMapping("/shopping/categories")
    public ResponseData shoppingCategories(String sort) {
        AllProductCateRequest allProductCateRequest = new AllProductCateRequest();
        allProductCateRequest.setSort(sort);

        AllProductCateResponse allProductCate = iProductCateService.getAllProductCate(allProductCateRequest);
        if (ShoppingRetCode.SUCCESS.getCode().equals(allProductCate.getCode())) {
            // 业务方法调用成功
            List<ProductCateDto> result = allProductCate.getProductCateDtoList();
            return new ResponseUtil().setData(result);
        }

        // 业务方法执行失败
        return new ResponseUtil().setErrorMsg(allProductCate.getMsg());
    }

}
